package com.base.controller;

import com.base.po.voip.Rewards;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.*;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 顺通记录
 *
 */
@Controller
@RequestMapping("/admin")
public class index_ZXRewards {
    @Autowired
    private RewardsService rewardsService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private SubscribersService subscribersService;
    @Autowired
    private WithdrawalsService withdrawalsService;


    // 自营商城订单首页
    @RequestMapping("/zxRewards")
    public ModelAndView index(HttpServletRequest request, String userid) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        List<Rewards> rewards = null;

        Page page = new Page();
        page.setCurrentPageNo(1);
        // 最多显示1000条
        page.setRecordTotal(1000);
        rewards = rewardsService.selectBetweenType(page, userid, 2, 2);

        if(rewards !=null && rewards.size()>0) {
            for(Rewards s : rewards) {
                // 下级用户充值
                Subscribers subscribers = subscribersService.selectByPrimaryKey(s.getPresentid());
                if(subscribers !=null) {
                    // 设置OEM以及所属商家的账户名
                    Users parent = usersService.selectByPrimaryKey(subscribers.getParentid());
                    Users shop = usersService.selectByPrimaryKey(subscribers.getShopid());
                    s.setSubParentid(parent.getUsername());
                    s.setSubShopid(shop.getUsername());

                    s.setSubMobile(subscribers.getMobile());
                    s.setSubUsername(subscribers.getUsername());
                    s.setSubDatastatus(subscribers.getDatastatus());
                    s.setSubCreatetime(subscribers.getCreatetime());
                    s.setSubEndtime(subscribers.getEndtime());
                }
            }
        }

        // 用户信息
        Subscribers subscribers = subscribersService.selectByPrimaryKey(userid);

        request.setAttribute("list", rewards);
        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("userid", userid);
        request.setAttribute("adminUserId", adminUserId);
        request.setAttribute("adminUsergrade", adminUsergrade);
        request.setAttribute("subscribers", subscribers);

        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_ZXRewards");
        return modelAndView;
    }

    @RequestMapping("/selectZXRewards")
    public ModelAndView selectZXRewards(HttpServletRequest request, String userid, Integer curPage) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        List<Rewards> rewards = null;

        Page page = new Page();
        page.setCurrentPageNo(curPage);
        // 最多显示1000条
        page.setRecordTotal(1000);
        rewards = rewardsService.selectBetweenType(page, userid, 2, 2);

        if(rewards !=null && rewards.size()>0) {
            for(Rewards s : rewards) {
                // 下级用户充值
                Subscribers subscribers = subscribersService.selectByPrimaryKey(s.getPresentid());
                if(subscribers !=null) {
                    Users parent = usersService.selectByPrimaryKey(subscribers.getParentid());
                    Users shop = usersService.selectByPrimaryKey(subscribers.getShopid());
                    s.setSubParentid(parent.getUsername());
                    s.setSubShopid(shop.getUsername());

                    s.setSubMobile(subscribers.getMobile());
                    s.setSubUsername(subscribers.getUsername());
                    s.setSubDatastatus(subscribers.getDatastatus());
                    s.setSubCreatetime(subscribers.getCreatetime());
                    s.setSubEndtime(subscribers.getEndtime());
                }
            }
        }

        // 用户信息
        Subscribers subscribers = subscribersService.selectByPrimaryKey(userid);

        request.setAttribute("list", rewards);
        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("userid", userid);
        request.setAttribute("adminUserId", adminUserId);
        request.setAttribute("adminUsergrade", adminUsergrade);
        request.setAttribute("subscribers", subscribers);

        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_ZXRewards");
        return modelAndView;
    }
}
